﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MeetingMinutesManager.Form;

namespace MeetingMinutesManager
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnClean_Click(object sender, EventArgs e)
        {
            Clean();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            String userName = txtUserName.Text.Trim();

            if (ResourceOperator.IsUserExists(userName))
            {
                Messenger.Show(Response, "User name already exists!");
                return;
            }

            String password = txtPassword.Text.Trim();
            String passwordConfirm = txtPasswordConfirm.Text.Trim();
            String email = txtEmail.Text.Trim();
            String team = ddlTeams.SelectedValue.Trim();
            String role = ddlRoles.SelectedValue.Trim();
            String manager = ddlManager.SelectedValue.Trim();

            if (userName.Equals(String.Empty) ||
                password.Equals(String.Empty) ||
                passwordConfirm.Equals(String.Empty) ||
                team.Equals(String.Empty) ||
                role.Equals(String.Empty))
            {
                Messenger.Show(Response, "Please fill all fields!");
                return;
            }

            if (!password.Equals(passwordConfirm))
            {
                Messenger.Show(Response, "Please input same password!");
                return;
            }

            if (!email.Contains('@') || !email.Contains('.'))
            {
                Messenger.Show(Response, "Email address needs to be XXX@XXX.com!");
                return;
            }

            try
            {
                ResourceOperator.AddNewUser(userName, password, email, team, role, manager);

                ResourceOperator.SendNewUserApprovalRequest(userName);
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
                return;
            }

            Clean();

            Messenger.Show(Response, "Please wait for approval then access the website.");

            //Session["user_name"] = userName;
            //Session.Timeout = 480;
            //Session["role"] = role;

            //Response.Redirect("Default.aspx");
        }

        private void Clean()
        {
            txtUserName.Text = String.Empty;
            txtPassword.Text = String.Empty;
            txtPasswordConfirm.Text = String.Empty;
            txtEmail.Text = String.Empty;
            ddlTeams.ClearSelection();
            ddlRoles.ClearSelection();
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }
    }
}
